package com.iot.framework.rabbitmq.service;


import com.alibaba.fastjson2.JSONObject;
import com.iot.common.enums.DeviceConnectType;
import com.iot.common.enums.DeviceParamType;
import com.iot.common.enums.DeviceStatusRecordType;
import com.iot.common.utils.DateUtils;
import com.iot.system.domain.DeviceDetails;
import com.iot.system.domain.DeviceParameterRecord;
import com.iot.system.domain.DeviceState;
import com.iot.system.domain.DeviceStateLogs;
import com.iot.system.service.IDeviceDetailsService;
import com.iot.system.service.IDeviceParameterRecordService;
import com.iot.system.service.IDeviceStateLogsService;
import com.iot.system.service.IDeviceStateService;
import com.iot.system.vo.DeviceParamVo;
import com.iot.system.vo.DeviceStateVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Date;
import java.util.Objects;

@Component
@Slf4j
public class DeviceParamService {

    @Resource
    private IDeviceDetailsService iDeviceDetailsService;

    @Resource
    private IDeviceParameterRecordService iDeviceParameterRecordService;



    //判断消息类型 存储设备参数 当前只有一个参数是温度 先这样写
    public void deviceParam(String message) {
        try {
            JSONObject jsonObject = JSONObject.parseObject(message);
            log.info("DeviceStateService_deviceParam_设备参数转换完成:{}",jsonObject);
            DeviceParamVo deviceParamVo = jsonObject.toJavaObject(DeviceParamVo.class);
            //获取设备详情
            DeviceDetails deviceDetails = iDeviceDetailsService.selectDeviceDetailsByToken(deviceParamVo.getCredentials());
            if(Objects.isNull(deviceDetails)|| Objects.isNull(deviceParamVo.getDeviceTemperature())){
                return;
            }
            DeviceParameterRecord deviceParameterRecord = new DeviceParameterRecord();
            deviceParameterRecord.setDeviceId(deviceDetails.getId());
            deviceParameterRecord.setParametersType(DeviceParamType.TEMPERATURE.getNumber());
            deviceParameterRecord.setParametersValue(deviceParamVo.getDeviceTemperature().toString());
            deviceParameterRecord.setCreateTime(new Date());
            iDeviceParameterRecordService.insertDeviceParameterRecord(deviceParameterRecord);
        }catch (Exception e){
            return;
        }
    }






}
